# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from spCrawl.items import Tencent


class Tencent2Spider(CrawlSpider):
    name = 'tencent2'
    allowed_domains = ['tencent.com']
    # https://hr.tencent.com/position.php?&start="
    start_urls = ['https://hr.tencent.com/position.php?&start=0']

    rules = (
        Rule(LinkExtractor(allow=r'start=\d+'), callback='parse_item', follow=True),
    )

    def parse_item(self, response):
        for each in response.xpath("//tr[@class='even'] | //tr[@class='odd']"):
            tencent = Tencent()
            tencent["name"] = each.xpath("./td[1]/a/text()").extract()[0]
            tencent["type"] = each.xpath("./td[2]/text()").extract()[0]
            tencent["count"] = each.xpath("./td[3]/text()").extract()[0]
            tencent["position"] = each.xpath("./td[4]/text()").extract()[0]
            tencent["time"] = each.xpath("./td[5]/text()").extract()[0]
            yield tencent
